package com.win.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Author:
 * @Date: 2024/7/18 16:33
 * @Description:
 */
public class Servlet_05_GetProperties extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 第一个/代表当前target 目录下的 打包资源路径：servlet-02-context
        //吧资源变成一个流去读取 resource下的db1
        InputStream is = getServletContext().getResourceAsStream("/WEB-INF/classes/db1.properties");
        Properties properties = new Properties();
        properties.load(is);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        resp.getWriter().println(username+"\t"+password);

        //吧资源变成一个流去读取 Java下的db2
        InputStream is2 = getServletContext().getResourceAsStream("/WEB-INF/classes/com/win/servlet/db2.properties");
        Properties properties2 = new Properties();
        properties2.load(is2);
        String username2 = properties2.getProperty("username");
        String password2 = properties2.getProperty("password");
        resp.getWriter().println(username2+"\t"+password2);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
